/**   
* @Title: DepartmentServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月24日 下午6:34:34 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.Date;
import java.util.List;

import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.DepartmentService;
import com.nebula.upms.service.model.po.UpmsDepartmentPO;
import com.nebula.upms.service.model.po.UpmsDepartmentPOExample;
import com.nebula.upms.service.support.constant.UpmsLocalFinal;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nebula.common.framework.BaseService;


/** 
* @ClassName: DepartmentServiceImpl 
* @Description: TODO() 
* @author zhanghui
* @date 2016年2月24日 下午6:34:34 
*  
*/
@Service("departmentService")
@Transactional
public class DepartmentServiceImpl extends BaseService implements DepartmentService {
	/*
	 * <p>Title: saveDepartment</p>
	 * <p>Description: </p>
	 * @param UpmsDepartmentPO
	 * @see com.nebula.upms.service.DepartmentService#saveDepartment(com.hbdiy.common.model.po.UpmsDepartmentPO)
	 */
	@Override
	public int saveDepartment(UpmsDepartmentPO UpmsDepartmentPO) {
		UpmsDepartmentPO.setDepartmentId(UUIDGenerator.random32UUID());
		UpmsDepartmentPO.setCreateTime(new Date().getTime());
		UpmsDepartmentPO.setType(UpmsLocalFinal.DEPARTMENT_TYPE_Y41);
		UpmsDepartmentPO.setIsDelete(false);
		
		int priority=0;
		UpmsDepartmentPOExample example1=new UpmsDepartmentPOExample();
		example1.setOrderByClause(" PRIORITY DESC");
		UpmsDepartmentPO last= UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example1);
		if(null!=last){
			priority=last.getPriority()+1;
		}
		
		UpmsDepartmentPO.setPriority(priority);
		int count=UpmsDs.instance().mybatis().ds().mapper().insert(UpmsDepartmentPO);
		return count;
	}

	/*
	 * <p>Title: getUpmsDepartmentPOByKey</p>
	 * <p>Description: </p>
	 * @param departmentId
	 * @return
	 * @see com.nebula.upms.service.DepartmentService#getUpmsDepartmentPOByKey(java.lang.String)
	 */
	@Override
	public UpmsDepartmentPO getUpmsDepartmentPOByKey(String departmentId) {
		UpmsDepartmentPO departmentPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsDepartmentPO.class,	 departmentId);
		return departmentPO;
	}

	/*
	 * <p>Title: updateDepartment</p>
	 * <p>Description: </p>
	 * @param UpmsDepartmentPO
	 * @see com.nebula.upms.service.DepartmentService#updateDepartment(com.hbdiy.common.model.po.UpmsDepartmentPO)
	 */
	@Override
	public int updateDepartment(UpmsDepartmentPO UpmsDepartmentPO) {
		int count=UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsDepartmentPO);
		return count;
	}

	/*
	 * <p>Title: removeDepartment</p>
	 * <p>Description: </p>
	 * @param departmentId
	 * @see com.nebula.upms.service.DepartmentService#removeDepartment(java.lang.String)
	 */
	@Override
	public int removeDepartment(String departmentId) {
		UpmsDepartmentPO UpmsDepartmentPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsDepartmentPO.class, departmentId);
		UpmsDepartmentPO.setIsDelete(true);
		int count=UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsDepartmentPO);
		//int count=UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsDepartmentPO.class, departmentId);
		return count;
	}

	/*
	 * <p>Title: moveup</p>
	 * <p>Description: </p>
	 * @param departmentId
	 * @return
	 * @see com.nebula.upms.service.DepartmentService#moveup(java.lang.String)
	 */
	@Override
	public int moveup(String departmentId) {
		UpmsDepartmentPO UpmsDepartmentPO = UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsDepartmentPO.class, departmentId);

		UpmsDepartmentPOExample example1 = new UpmsDepartmentPOExample();
		example1.createCriteria().andIsDeleteEqualTo(false);
		example1.setOrderByClause(" PRIORITY ASC");
		List<UpmsDepartmentPO> list = UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
		int index = 0;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getDepartmentId().equals(departmentId)) {
				if (i == 0) {
					index = 0;
				} else {
					index = i - 1;
				}
				break;
			}
		}

		UpmsDepartmentPO prev = list.get(index);
		int priority = prev.getPriority();
		prev.setPriority(UpmsDepartmentPO.getPriority());
		UpmsDepartmentPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(prev);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsDepartmentPO);
		return 1;
	}

	/*
	 * <p>Title: movedown</p>
	 * <p>Description: </p>
	 * @param departmentId
	 * @return
	 * @see com.nebula.upms.service.DepartmentService#movedown(java.lang.String)
	 */
	@Override
	public int movedown(String departmentId) {
		UpmsDepartmentPO UpmsDepartmentPO = UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsDepartmentPO.class, departmentId);

		UpmsDepartmentPOExample example1 = new UpmsDepartmentPOExample();
		example1.createCriteria().andIsDeleteEqualTo(false);
		example1.setOrderByClause(" PRIORITY ASC");
		List<UpmsDepartmentPO> list = UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
		int index = 0;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getDepartmentId().equals(departmentId)) {
				if (i == (list.size() - 1)) {
					index = list.size() - 1;
				} else {
					index = i + 1;
				}
				break;
			}
		}

		UpmsDepartmentPO next = list.get(index);
		int priority = next.getPriority();
		next.setPriority(UpmsDepartmentPO.getPriority());
		UpmsDepartmentPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(next);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsDepartmentPO);
		return 1;
	}
	
}
